var e = require("../../@babel/runtime/helpers/interopRequireDefault"), t = e(require("../../@babel/runtime/regenerator")), a = require("../../@babel/runtime/helpers/asyncToGenerator"), n = e(require("../../http/carApi")), o = e(require("../../http/system")), s = require("../../utils/tool"), r = e(require("../../http/robot")), i = getApp(), c = (require("../../utils/api.js"), 
new n.default(), new o.default()), u = require("../../http/indexApi.js"), h = new r.default(), l = require("../../http/activityApi.js");

Page({
    fromUserID: 0,
    shareFrom: 0,
    redirectTo: null,
    eventChannel: null,
    loginInfo: null,
    data: {
        bgImgUrl: "http://newimg.ap1983.com/prod/20211011/56a0bf18bc0646f389b3e06bf8aec868.png",
        loginImgUrl: "http://newimg.ap1983.com/prod/20211011/314672810970437fab96c340f6440d7b.png",
        systemInfo: i.data.systemInfo,
        show: !1,
        weChatData: {
            encryptedData: "",
            iv: ""
        },
        loginAuthCode: "",
        showCaptcha: !0,
        captchaType: "blockPuzzle"
    },
    onLoad: function(e) {
        var t = i.data.systemInfo, a = 750 * t.screenHeight / t.screenWidth;
        if (a <= 1624) this.setData({
            bgSize: "100% ".concat(1624, "rpx"),
            bgPosi: "top center"
        }); else {
            var n = 750 * a / 1624;
            this.setData({
                bgSize: "".concat(n, "rpx ").concat(a, "rpx"),
                bgPosi: "center center"
            });
        }
        e.loginAuthCode && this.setData({
            loginAuthCode: e.loginAuthCode
        }), this.redirectTo = e.redirectTo, this.redirectTo && (this.redirectTo = decodeURIComponent(this.redirectTo)), 
        e.shareFrom && (this.shareFrom = e.shareFrom), e.fromUserID && (this.fromUserID = e.fromUserID), 
        this.eventChannel = this.getOpenerEventChannel();
    },
    onShow: function() {
        var e = this;
        return a(t.default.mark(function a() {
            var n;
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.next = 2, (0, s.promising)(wx.login)();

                  case 2:
                    n = t.sent, e.setData({
                        code: n.code
                    }), i.clearLoginInfo(), i.global.removeLoginInfo();

                  case 6:
                  case "end":
                    return t.stop();
                }
            }, a);
        }))();
    },
    switchType: function() {
        this.setData({
            showCaptcha: !1
        }), this.setData({
            captchaType: "blockPuzzle" == this.data.captchaType ? "clickWord" : "blockPuzzle"
        }), this.setData({
            showCaptcha: !0
        }), this.selectComponent("#captcha").show();
    },
    closeNewUserGiftDialogOnTap: function() {
        this.closeUI();
    },
    showCaptcha: function() {
        this.setData({
            show: !0
        }), this.selectComponent("#captcha").show();
    },
    oauth: function(e) {
        var n, o, r = this;
        e.detail.encryptedData ? (this.setData({
            weChatData: {
                encryptedData: e.detail.encryptedData,
                iv: e.detail.iv
            }
        }), wx.showLoading({
            mask: !0,
            title: "加载中…"
        }), wx.checkSession({
            success: (o = a(t.default.mark(function e(a) {
                return t.default.wrap(function(e) {
                    for (;;) switch (e.prev = e.next) {
                      case 0:
                        console.log(a), wx.hideLoading(), r.showCaptcha();

                      case 3:
                      case "end":
                        return e.stop();
                    }
                }, e);
            })), function(e) {
                return o.apply(this, arguments);
            }),
            fail: (n = a(t.default.mark(function e(a) {
                var n;
                return t.default.wrap(function(e) {
                    for (;;) switch (e.prev = e.next) {
                      case 0:
                        return console.log(a), e.next = 3, (0, s.promising)(wx.login)();

                      case 3:
                        n = e.sent, r.setData({
                            code: n.code
                        }), r.register();

                      case 6:
                      case "end":
                        return e.stop();
                    }
                }, e);
            })), function(e) {
                return n.apply(this, arguments);
            })
        })) : wx.showToast({
            title: "用户取消授权",
            icon: "none",
            duration: 2e3
        });
    },
    register: function(e) {
        var n = this;
        return a(t.default.mark(function a() {
            var o, r, c;
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.prev = 0, wx.showLoading({
                        mask: !0,
                        title: "加载中…"
                    }), console.log("sourceInfo1:", i.data.sourceInfo), t.next = 5, u.wxPhoneLogin(n.data.code, n.data.weChatData.encryptedData, n.data.weChatData.iv, n.shareFrom, e.detail.captchaVerification, i.data.sourceInfo);

                  case 5:
                    if (o = t.sent, i.globalData.token = o.data.token, i.data.loginInfo = o.data, wx.setStorageSync("token", o.data.token), 
                    wx.setStorage({
                        key: "LoginInfo",
                        data: i.data.loginInfo
                    }), !i.data.loginInfo.userInfo.lastLoginWxTime && n.subscribeSuccess && u.subscribeNotice({
                        messageType: 8,
                        messageId: ""
                    }).then(function(e) {}).catch(function(e) {}), i.global.setLoginInfo(o.data.userInfo), 
                    i.linkSocket(), n.data.loginAuthCode && n.robotLogin(n.data.loginAuthCode), wx.hideLoading(), 
                    0 != o.data.isRegisteredStatus) {
                        t.next = 20;
                        break;
                    }
                    return t.next = 18, l.newUserGift();

                  case 18:
                    r = t.sent, n.setData({
                        newUserGift: r.data
                    });

                  case 20:
                    n.data.newUserGift || ((0, s.toastToPage)("登录成功"), setTimeout(function() {
                        n.closeUI();
                    }, 1e3)), t.next = 29;
                    break;

                  case 23:
                    return t.prev = 23, t.t0 = t.catch(0), t.next = 27, (0, s.promising)(wx.login)();

                  case 27:
                    c = t.sent, n.setData({
                        code: c.code
                    });

                  case 29:
                    return t.prev = 29, wx.hideLoading(), t.finish(29);

                  case 32:
                  case "end":
                    return t.stop();
                }
            }, a, null, [ [ 0, 23, 29, 32 ] ]);
        }))();
    },
    robotLogin: function(e) {
        return a(t.default.mark(function a() {
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.prev = 0, t.next = 3, h.robotLogin({
                        loginAuthCode: e,
                        memberId: i.data.loginInfo.memberId
                    });

                  case 3:
                    t.sent, (0, s.toastToPage)("机器登陆成功"), t.next = 9;
                    break;

                  case 7:
                    t.prev = 7, t.t0 = t.catch(0);

                  case 9:
                  case "end":
                    return t.stop();
                }
            }, a, null, [ [ 0, 7 ] ]);
        }))();
    },
    lookTicketOnTap: function() {
        this.eventChannel.emit && this.eventChannel.emit("onLoginSuccess", {
            loginInfo: i.data.loginInfo
        }), wx.redirectTo({
            url: "/packages/vip/page/card-ticket-bag/card-ticket-bag"
        });
    },
    closeUI: function() {
        var e = this;
        this.eventChannel.emit && this.eventChannel.emit("onLoginSuccess", {
            loginInfo: i.data.loginInfo
        }), this.redirectTo ? i.isTabPage(this.redirectTo) ? wx.switchTab({
            url: this.redirectTo
        }) : wx.redirectTo({
            url: this.redirectTo,
            fail: function(t) {
                wx.switchTab({
                    url: e.redirectTo
                });
            }
        }) : i.returnUI();
    },
    subscribeMessageThenCloseUI: function() {
        var e = this;
        c.getServerTime().then(function(t) {
            wx.requestSubscribeMessage({
                tmplIds: [ "vBPr22DDXOhYMu7o3hc3k055YKUvYSmncOQjkcVzLE0", "hdkwX1PFKY08Wrw3z18S6oFpn2SeoyToVW4qGW2L-4w", "C1t5mpxs6FRapLx2Ht1gnmwpDyKwJj1HkEzWJ_mv6R0" ],
                success: function(e) {
                    console.log("success:", e), i.showToast("订阅成功");
                },
                fail: function(e) {
                    console.log("fail:", e), i.showToast("订阅失败");
                },
                complete: function(t) {
                    i.data.loginInfo.userInfo.lastLoginWxTime = "subscribe", e.closeUI();
                }
            });
        }).catch(function(e) {});
    },
    subscribeOnAccept: function() {
        var e = this;
        wx.requestSubscribeMessage({
            tmplIds: [ "C1t5mpxs6FRapLx2Ht1gnmwpDyKwJj1HkEzWJ_mv6R0" ],
            success: function(t) {
                e.subscribeSuccess = !0;
            }
        });
    }
});